<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;

class MedicalAuxiliary extends Model
{
    protected $table = 'st_medical_auxiliarycheck';

    protected $fillable = [
        'record_id', 'blood', 'insulin', 'peptide_c', 'ogtt', 'saccharify', 'diabetes_antibody', 'acr', 'uaer',
        'urine_routine', 'biochemical_examination', 'liver_function', 'kidney', 'electrolyte', 'tg_chohdlldl',
        'bt_paba', 'thyroid_function_one', 'thyroid_function_two', 'thyroid_function_three', 'plasma_acth',
        'blood_cortisol',
        'endocrine_examination', 'growth_hormone', 'insulin_growth_factor', 'blood_routine', 'blood_gas_analysis',
        'blood_coagulation_two', 'dexamethasone_suppression', 'hydroxyvitamin', 'uca_up', 'collagen_degradation',
        'iconography', 'remark','blood_coagulation_one',
    ];

    //  关联
    public function medicalRecord()
    {
        return $this->belongsTo(MedicalRecord::class, 'record_id');
    }

    public function printItems(MedicalRecord $record)
    {
        if (empty($record->print_auxiliary))
            return null;

        $fields = Arr::only(app('field')->get('auxiliary.print'), $record->print_auxiliary);

        $fields = array_merge(Arr::pluck($fields, 'field'), ['remark']);

        if (!app('scene')->isWestern()) {
            if (in_array('biochemical_examination', $fields)) {
                $fields[] = 'bt_paba';
            }

            if (in_array('thyroid_function_two', $fields)) {
                $fields[] = 'thyroid_function_three';
            }
        }

        return $this->where(['record_id' => $record->id])->first($fields);
    }

    public function getBloodAttribute($value)
    {
        return $value ? json_decode($value, true) : '';
    }

    public function getInsulinAttribute($value)
    {
        return $value ? json_decode($value, true) : '';
    }

    public function getPeptideCAttribute($value)
    {
        return $value ? json_decode($value, true) : '';
    }

    public function getOgttAttribute($value)
    {
        return $value ? json_decode($value, true) : '';
    }

    public function getSaccharifyAttribute($value)
    {
        return $value ? json_decode($value, true) : '';
    }

    public function getDiabetesAntibodyAttribute($value)
    {
        return $value ? json_decode($value, true) : '';
    }

    public function getAcrAttribute($value)
    {
        return $value ? json_decode($value, true) : '';
    }

    public function getUaerAttribute($value)
    {
        return $value ? json_decode($value, true) : '';
    }

    public function getUrineRoutineAttribute($value)
    {
        return $value ? json_decode($value, true) : '';
    }

    public function getBiochemicalExaminationAttribute($value)
    {
        return $value ? json_decode($value, true) : '';
    }

    public function getLiverFunctionAttribute($value)
    {
        return $value ? json_decode($value, true) : '';
    }

    public function getKidneyAttribute($value)
    {
        return $value ? json_decode($value, true) : '';
    }

    public function getElectrolyteAttribute($value)
    {
        return $value ? json_decode($value, true) : '';
    }

    public function getTgChohdlldlAttribute($value)
    {
        return $value ? json_decode($value, true) : '';
    }

    public function getBtPabaAttribute($value)
    {
        return $value ? json_decode($value, true) : '';
    }

    public function getThyroidFunctionOneAttribute($value)
    {
        return $value ? json_decode($value, true) : '';
    }

    public function getThyroidFunctionTwoAttribute($value)
    {
        return $value ? json_decode($value, true) : '';
    }

    public function getThyroidFunctionThreeAttribute($value)
    {
        return $value ? json_decode($value, true) : '';
    }

    public function getPlasmaActhAttribute($value)
    {
        return $value ? json_decode($value, true) : '';
    }

    public function getBloodCortisolAttribute($value)
    {
        return $value ? json_decode($value, true) : '';
    }

    public function getEndocrineExaminationAttribute($value)
    {
        return $value ? json_decode($value, true) : '';
    }

    public function getGrowthHormoneAttribute($value)
    {
        return $value ? json_decode($value, true) : '';
    }

    public function getInsulinGrowthFactorAttribute($value)
    {
        return $value ? json_decode($value, true) : '';
    }

    public function getBloodRoutineAttribute($value)
    {
        return $value ? json_decode($value, true) : '';
    }

    public function getBloodGasAnalysisAttribute($value)
    {
        return $value ? json_decode($value, true) : '';
    }

    public function getBloodCoagulationOneAttribute($value)
    {
        return $value ? json_decode($value, true) : '';
    }

    public function getBloodCoagulationTwoAttribute($value)
    {
        return $value ? json_decode($value, true) : '';
    }

    public function getDexamethasoneSuppressionAttribute($value)
    {
        return $value ? json_decode($value, true) : '';
    }

    public function getHydroxyvitaminAttribute($value)
    {
        return $value ? json_decode($value, true) : '';
    }

    public function getUcaUpAttribute($value)
    {
        return $value ? json_decode($value, true) : '';
    }

    public function getCollagenDegradationAttribute($value)
    {
        return $value ? json_decode($value, true) : '';
    }

    public function getIconographyAttribute($value)
    {
        return $value ? json_decode($value, true) : '';
    }
}
